package org.rugby.online.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import org.rugby.online.core.season.RboDay;
import org.rugby.online.core.season.RboSeason;
import org.rugby.online.core.season.events.RboDayEvent;
import org.rugby.online.core.season.events.RboDayEventType;

public class DefaultSeason extends DefaultObject implements RboSeason {

	/**
	 * Default season implementation
	 */
	private static final long serialVersionUID = 7309832951551452462L;
	
	/**
	 * The season number
	 */
	private int seasonNumber;
	
	/**
	 * Days in the season
	 */
	private List<RboDay> seasonDays;
	
	/**
	 * Match days in the season
	 */
	private List<RboDay> matchDays;
	
	/**
	 * Match days in the season
	 */
	private List<RboDay> cupDays;
	
	/**
	 * Training days of the season
	 */
	private List<RboDay> trainingDays;
	
	/**
	 * Starting day
	 */
	private Date startingDate;
	
	/**
	 * Constructor
	 * @param name the name of the season
	 */
	public DefaultSeason(String name, int number) {
		super();
		this.setName(name);
		this.seasonNumber = number;
		this.seasonDays = new ArrayList<RboDay>(RboSeason.NUMBER_OF_DAYS);
	}

	@Override
	public int getSeasonNumber() {
		return this.seasonNumber;
	}

	@Override
	public List<RboDay> getDays() {
		return this.seasonDays;
	}

	@Override
	public List<RboDay> getMatchDays() {
		if (this.matchDays == null) {
			
			// Match days have not been initialized, then do it
			matchDays = new ArrayList<RboDay>();
			
			for (RboDay day : seasonDays) {
				// searching for match days
				boolean containsMatch = false;
				for (RboDayEvent event : day.getDayEvents()) {
					if (event.getDayEventType().equals(RboDayEventType.MATCH)) {
						containsMatch = true;
						break;
					}
				}
				if (containsMatch) {
					matchDays.add(day);
				}
			}
			
		}
		return this.matchDays;
	}

	@Override
	public List<RboDay> getCupDays() {
		if (this.cupDays == null) {
			
			// Match days have not been initialized, then do it
			cupDays = new ArrayList<RboDay>();
			
			for (RboDay day : seasonDays) {
				// searching for match days
				boolean containsMatch = false;
				for (RboDayEvent event : day.getDayEvents()) {
					if (event.getDayEventType().equals(RboDayEventType.CUP)) {
						containsMatch = true;
						break;
					}
				}
				if (containsMatch) {
					cupDays.add(day);
				}
			}
			
		}
		return this.cupDays;
	}

	@Override
	public List<RboDay> getTrainingDays() {
		if (this.trainingDays == null) {
			
			// Match days have not been initialized, then do it
			trainingDays = new ArrayList<RboDay>();
			
			for (RboDay day : seasonDays) {
				// searching for match days
				boolean containsMatch = false;
				for (RboDayEvent event : day.getDayEvents()) {
					if (event.getDayEventType().equals(RboDayEventType.TRAINING)) {
						containsMatch = true;
						break;
					}
				}
				if (containsMatch) {
					trainingDays.add(day);
				}
			}
			
		}
		return this.trainingDays;
	}

	@Override
	public void init() {
		this.startingDate = new Date();
	}

	@Override
	public void terminate() {
		// TODO Auto-generated method stub

	}

	@Override
	public int currentDay() {
		Calendar now = Calendar.getInstance(Locale.FRANCE);
		Calendar start = Calendar.getInstance(Locale.FRANCE);
		start.setTime(startingDate);
		return now.compareTo(start) / (1000 * 60 * 60 * 24);
	}

}
